package com.aaa.bbb.mymaintain.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import com.aaa.bbb.mymaintain.entity.Employee;
import com.aaa.bbb.mymaintain.entity.IssuesEmployee;
import com.aaa.bbb.mymaintain.form.*;
import com.github.pagehelper.PageInfo;;
import com.aaa.bbb.common.http.ResponseResult;
import com.aaa.bbb.mymaintain.service.IssuesEmployeeSer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author max
 * @since 2021-07-29
 */

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/issues_employee")
public class IssuesEmployeeCtl {
    @Autowired
    private IssuesEmployeeSer ser;

    @PostMapping("/getsubmitissue")
    public ResponseResult<PageInfo<Map<String, Object>>> getSubmitIssue(@RequestBody @Valid IssuesEmployeeForm form) {
        PageInfo<Map<String, Object>> pager = ser.queryPagerByCond(form.getUsername(),
                form.getPageNum(),
                form.getPageSize()
        );
        return ResponseResult.getSuccessResult(pager);
    }

    @PostMapping("/getconfirmissue")
    public ResponseResult<PageInfo<Map<String, Object>>>getConfirmIssue(@RequestBody @Valid IssuesEmployeeForm form) {
        PageInfo<Map<String, Object>> pager = ser.queryPagerByCond1(form.getUsername(),
                "未检查是否维修合格",
                form.getPageNum(),
                form.getPageSize()
        );
        return ResponseResult.getSuccessResult(pager);
    }

//    @PostMapping("/delete")
//    public ResponseResult<Boolean> delete(@RequestBody @Valid StudentDeleteForm form) {
//        boolean res = ser.removeByIds(form.getIds());
//        if (res) {
//            return ResponseResult.getSuccessResult(true, "A004", null);
//        } else {
//            return ResponseResult.getMessageResult(false, "A005");
//        }
//    }
//
    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody @Validated({IssueEmployeeAddForm.Base.class}) IssueEmployeeAddForm form) {
        IssuesEmployee issuesEmployee = BeanUtil.toBean(form, IssuesEmployee.class);
        boolean res = ser.checkAndInsert(issuesEmployee,form);
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }

//    @PostMapping("/findbyid")
//    public ResponseResult<IssuesEmployee> findByid(@RequestBody @Validated({IssueEmployeeUpdateForm.FindbyId.class}) IssueEmployeeUpdateForm form) {
//        IssuesEmployee issuesEmployee = ser.getById(form.getIssue_id());
//        return ResponseResult.getSuccessResult(issuesEmployee);
//    }

    @PostMapping("/update")
    public void update(@RequestBody @Validated({IssueEmployeeUpdateForm.Update.class}) IssueEmployeeUpdateForm form) {
        ser.updateIssueEmployee(form.getIssue_id(),form.getState());
    }

    @PostMapping("/maintainunqualified")
    public void maintainUnQualified(@RequestBody @Validated({IssueEmployeeUpdateForm.Update.class}) IssueEmployeeUpdateForm form) {
        ser.maintainUnQualified1(form.getIssue_id(),form.getState(),form.getReason());
    }

    @PostMapping("/querymessage")
    public ResponseResult<Map<String,String>> queryMessage(@RequestBody @Valid QueryMessageForm form) {
        return ResponseResult.getSuccessResult(ser.queryMessageById(form.getIssue_id()));
    }

    @Value("${dirPath}")
    private String dirPath;

    @PostMapping("/upload")
    public Map<String,String> upload(@RequestParam("files") MultipartFile[] files,@RequestParam("explanation") String[] explanations) throws IOException {
//        log.debug("picFile1111:"+picFile);
        String path = "";
        String explanation = "";
        for(int i = 0;i<files.length;i++){
            MultipartFile picFile = files[i];
            //得到文件原始文件名 a.jpg
            String filename = picFile.getOriginalFilename();
            //得到后缀名  从最后一个.出现的位置截取到最后
            String suffix = filename.substring(filename.lastIndexOf("."));
            //得到唯一文件名 UUID.randomUUID()得到一个唯一标识
            filename = UUID.randomUUID() + suffix;
            path+=filename+";";
            explanation+=explanations[i]+";";
//        log.debug("文件名:"+filename);
            //准备保存图片的文件夹路径
//        String dirPath = "D:\\项目框架\\springbootapi\\src\\main\\resources\\images";
            File dirFile = new File(dirPath);
            //如果文件夹不存在,则创建
            if (!dirFile.exists()) {
                dirFile.mkdirs(); //创建文件夹
            }
            //得到文件的完整路径
            String filePath = dirPath+"/"+filename;
            //把文件保存到此路径
            picFile.transferTo(new File(filePath));
//        log.debug("文件保存完成,请检查文件是否存在!"+filePath);
//        log.debug("文件名称!"+filename);
//        return JsonResult.ok(filename);
        }
        Map<String,String> map = new HashMap<>();
        map.put("path",path);
        map.put("explanation",explanation);
        return map;
    }

}



